﻿using IOPerformanceTest.Tests;
using IOPerformanceTest.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IOPerformanceTest
{
    class TestExecutor
    {
        public static void Run(string[] args)
        {
            Run(Config.Parse(args));
        }

        public static void Run(Config config = null)
        {
            config = config ?? new Config();
            StopWatch sw = new StopWatch();
            DataManager dc = new DataManager();
            ITestable test = config.GetAlgoritm();
            Console.WriteLine("Test Algorithm: " + test.GetType().Name);

            test.PrepareData(config);

            DateTime dt1 = DateTime.Now;
            for (int i = 0; i < config.RunningTimes; i++)
            {
                sw.Start();
                test.Test();
                sw.Stop();
                dc.Add("WritingTime", sw.TotalTime);
                dc.Add("WritingSpeed", config.SizeInMB / sw.TotalTime);
                Console.Write(i < config.RunningTimes - 1 ? "." : ".\n");
            }
            test.FinishTest();
            DateTime dt2 = DateTime.Now;

            Console.WriteLine((dt2 - dt1).TotalSeconds.ToString("0.00"));
            foreach (string key in dc.GetKeys())
            {
                Console.WriteLine($"{key}: {dc.GetMedian(key)} ms. Speed: {(1.024 * config.SizeInMB / dc.GetMedian(key)).ToString("0.00")} GB/s.");
            }

            dc.Show();
        }

        public static byte[] GetBytes(int mb)
        {
            return new byte[mb * 1024 * 1024];
        }
    }
}
